package com.sofwin.service.impl;

import com.github.pagehelper.PageInfo;
import com.sofwin.mapper.SysDeptMapper;
import com.sofwin.pojo.SysDept;
import com.sofwin.pojo.SysDeptExample;
import com.sofwin.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ClassName: DeptServiceImpl
 * Description: TODO
 * date: 2022/4/27 9:45 上午
 *
 * @author andyliu
 * @qq 115176513
 * @since JDK 1.8
 */
@Service
@Transactional
public class DeptServiceImpl implements DeptService {
    @Autowired
    private SysDeptMapper mapper;

    @Override
    public List<SysDept> queryDepts(SysDept dept) {
        SysDeptExample example = new SysDeptExample();
        SysDeptExample.Criteria criteria = example.createCriteria();
        if(dept!=null){
            if(dept.getDeptName()!=null && !"".equals(dept.getDeptName())){
                criteria.andDeptNameLike("%"+dept.getDeptName()+"%");
            }
            if(dept.getIsDalete()!=null){
                criteria.andIsDaleteEqualTo(dept.getIsDalete());
            }
        }
        List<SysDept> depts = mapper.selectByExample(example);
        return depts;
    }

    @Override
    public SysDept queryById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean saveOrUpdate(SysDept dept) {
        if(dept.getDeptId()!=null){
            return mapper.updateByPrimaryKeySelective(dept)
                    ==1?true:false;
        }
         return mapper.insertSelective(dept)==1?true:false;
    }

    @Override
    public boolean removeById(Integer id) {
        return mapper.deleteByPrimaryKey(id)==1?true:false;
    }

    @Override
    public boolean removeByIds(Integer[] ids) {
        int num=0;
        for(Integer id:ids){
            if(removeById(id)){
                num++;
            }
        }
        return num==ids.length?true:false;
    }
}
